clear
clear matrix
capture log close
set rmsg on
set more off
set more 1
set matsize 800
set scrollbufsize 500000
set mem 7g

********************************************************************************
*****                  Setting paths to file directories                    ****
********************************************************************************

* Desktop path - designate as needed *

global code "XXXXX\code_for_publication"

********************************************************************************
* Cole, Damm, Hackney, and Suleymanov (2024) *                                  
* "It's not (only) personal, it's business: Personal bankruptcy exemptions and business credit" *
* Review of Finance *
********************************************************************************

clear all
clear matrix

***************************************************************
************************   Table 2   **************************
***************************************************************

* open file *

	use "$code/total_tract.dta", clear
	
* identify main sample

	reghdfe logamt4 ln_homestead_dec31 wlnpop wunempr wlfpr wmedage wlnmfi wminpop hpi_growth if year>2005, a( msa_year2  geoid_tract  ) vce (cluster fips_st)
	gen sample=e(sample)

* Get stats for tract vars

	tabstat sblamt4 sblamt5 homestead_dec31 wpop wunempr wlfpr wmedage wmfi wminpop hpi_growth ///
	if e(sample), stat(n mean p50 sd min max) columns(stat)

* open file	

	use "$code/total_county.dta", clear
	global wcontrols wlnpop1 wlnmfi wmed_age wunempr wlfpr1 wminority_pop  hpi_growth

* identify sample for county-level variables

	ppmlhdfe n1_19 ln_homestead_dec31 $wcontrols if year>2005, absorb(fips msa_year2) cluster(fips_st)

* Get stats for county vars	

	tabstat n1_19 n20_49 n50_249 n250_499 empendq4_total0_19_ empendq4_total20_49_  ///
	empendq4_total50_249_ empendq4_total250_499_ total_sba_amount total_sba_loan_amount total_sba_rev_amount ///
	sbo_loan_vol bus_loan_vol fintech total_nonbank ///
	if e(sample), stat(n mean p50 sd min max) columns(stat)

***************************************************************
************************   Table 3   **************************
***************************************************************

* open file

	use "$code/state_year_predictive.dta", clear

*************** Panel A: Overall Determinants *****************

	reghdfe F.ln_homestead_dec31 logpop wunempr wlfpr wmedage logmfi wminpop hpi_growth2 if year>2005, absorb(fips_st year) cluster(fips_st)
	outreg2 using Table3a, label addtext(Tract FE , Yes, MSA * Year , Yes) tstat bdec(3) excel replace
	reghdfe homestead_growth1 logpop wunempr wlfpr wmedage logmfi wminpop hpi_growth2 if year>2005, absorb(fips_st year) cluster(fips_st)
	outreg2 using Table3a, label addtext(Tract FE , Yes, MSA * Year , Yes) tstat bdec(3) excel append

********* Panel B: Border vs. Non-Border Determinants *********

	reghdfe F.ln_homestead_dec31 logpop_nonborder wunempr_nonborder wlfpr_nonborder wmedage_nonborder logmfi_nonborder wminpop_nonborder hpi_growth2_nonborder logpop_border wunempr_border wlfpr_border wmedage_border logmfi_border hpi_growth2_border wminpop_border if year>2005, absorb(fips_st year) cluster(fips_st)
	outreg2 using Table3b, label addtext(Tract FE , Yes, MSA * Year , Yes) tstat bdec(3) excel append
	reghdfe homestead_growth1 logpop_nonborder wunempr_nonborder wlfpr_nonborder wmedage_nonborder logmfi_nonborder wminpop_nonborder hpi_growth2_nonborder logpop_border wunempr_border wlfpr_border wmedage_border logmfi_border hpi_growth2_border wminpop_border if year>2005, absorb(fips_st year) cluster(fips_st)
	outreg2 using Table3b, label addtext(Tract FE , Yes, MSA * Year , Yes) tstat bdec(3) excel append
	
*********** Panel C: Exemptions and Out-Migration ***********	

* open file

	use "$code/total_county.dta", clear
	
	global wcontrols wlnpop1 wlnmfi wmed_age wunempr wlfpr1 wminority_pop hpi_growth
	global treatment ln_homestead_dec31	

	reghdfe out_movers_pop $treatment $wcontrols  if year>2005, a(cou_id year) vce (cluster fips_st)
	outreg2 using Table3c, label addtext(County FE , Yes, Year , Yes) tstat bdec(3) excel replace
	reghdfe movers_pop $treatment $wcontrols  if year>2005, a(cou_id year) vce (cluster fips_st)
	outreg2 using Table3c, label addtext(County FE , Yes, Year , Yes) tstat bdec(3) excel append
	
***************************************************************
************************   Table 4   **************************	
***************************************************************

* open file

	use "$code/total_tract.dta", clear
	global controls2 wlnpop wunempr wlfpr wmedage wlnmfi wminpop hpi_growth

*************** Panel A: Baseline Specification *****************		
		
	reghdfe logamt4 ln_homestead_dec31 if year>2005 , a( msa_year2  geoid_tract  ) vce (cluster fips_st)
	outreg2 using Table4a, label addtext(Tract FE , Yes, MSA * Year , Yes) tstat bdec(3) excel replace
	reghdfe logamt5 ln_homestead_dec31 if year>2005 , a( msa_year2  geoid_tract  ) vce (cluster fips_st)
	outreg2 using Table4a, label addtext(Tract FE , Yes, MSA * Year , Yes) tstat bdec(3) excel append
	reghdfe logamt4 ln_homestead_dec31 $controls2 if year>2005 , a( msa_year2  geoid_tract  ) vce (cluster fips_st)
	outreg2 using Table4a, label addtext(Tract FE , Yes, MSA * Year , Yes) tstat bdec(3) excel append
	reghdfe logamt5 ln_homestead_dec31 $controls2 if year>2005 , a( msa_year2  geoid_tract  ) vce (cluster fips_st)
	outreg2 using Table4a, label addtext(Tract FE , Yes, MSA * Year , Yes) tstat bdec(3) excel append

*************** Panel B: Tracts near border ***************** 
		
	reghdfe logamt4 ln_homestead_dec31 $controls2  if year>2005 & census_distance<20, a(census_pair_year  geoid_tract) vce (cluster fips_st)
	outreg2 using Table4b, label addtext(Tract FE , Yes, Border Pair * Year , Yes) tstat bdec(3) excel replace
	reghdfe logamt5 ln_homestead_dec31 $controls2  if year>2005 & census_distance<20, a(census_pair_year  geoid_tract) vce (cluster fips_st)
	outreg2 using Table4b, label addtext(Tract FE , Yes, Border Pair * Year , Yes) tstat bdec(3) excel append

***************************************************************
************************   Table 5   **************************	
***************************************************************

* open file

	use "$code/total_stack_tract.dta", clear

************* Panel A: Stacked DiD Specification ***************

* identify controls

	global controls2 wlnpop wunempr wlfpr wmedage wlnmfi wminpop hpi_growth

* Baseline

	reghdfe logamt4 ln_homestead_dec31 $controls2, absorb( c_tract c_msa_year2) cluster( state_stack )
	outreg2 using Table5a, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel replace
	reghdfe logamt5 ln_homestead_dec31 $controls2, absorb( c_tract c_msa_year2) cluster( state_stack )
	outreg2 using Table5a, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append

****************** Panel B: Size of Increase ********************

* Heterogeneity by size of increase

	reghdfe logamt4 treat_post_small treat_post_medium treat_post_large $controls2, absorb( c_tract c_msa_year2) cluster( state_stack )
	outreg2 using Table5b, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort FE , Yes) tstat bdec(3) excel replace
	reghdfe logamt5 treat_post_small treat_post_medium treat_post_large $controls2, absorb( c_tract c_msa_year2) cluster( state_stack )
	outreg2 using Table5b, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort FE , Yes) tstat bdec(3) excel append

***************** Panel C: Timing of Increase *******************

* Heterogeneity by timing of increase

* Treatment cohorts 2006-2009

	reghdfe logamt4 ln_homestead_dec31 $controls2 if stack<2010, absorb( c_tract c_msa_year2) cluster( state_stack )
	outreg2 using Table5c, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel replace
	reghdfe logamt5 ln_homestead_dec31 $controls2 if stack<2010, absorb( c_tract c_msa_year2) cluster( state_stack )
	outreg2 using Table5c, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append

* Treatment cohorts 2010-2016

	reghdfe logamt4 ln_homestead_dec31 $controls2 if stack>=2010, absorb( c_tract c_msa_year2) cluster( state_stack )
	outreg2 using Table5c, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	reghdfe logamt5 ln_homestead_dec31 $controls2 if stack>=2010, absorb( c_tract c_msa_year2) cluster( state_stack )
	outreg2 using Table5c, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append

***************************************************************
************************   Table 6   **************************	
***************************************************************

* Treatment tracts along borders

	reghdfe logamt4 ln_homestead_dec31 $controls2 if treated_this==1 & ln_neighbor!=., absorb( c_tract year_stack) cluster( state_stack )
	outreg2 using Table6, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	reghdfe logamt5 ln_homestead_dec31 $controls2 if treated_this==1 & ln_neighbor!=., absorb( c_tract year_stack) cluster( state_stack )
	outreg2 using Table6, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append

* Control tracts along borders
	
	reghdfe logamt4 ln_neighbor_homestead $controls2 if untreated_this==1, absorb( c_tract year_stack) cluster( state_stack )
	outreg2 using Table6, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	reghdfe logamt5 ln_neighbor_homestead $controls2 if untreated_this==1, absorb( c_tract year_stack) cluster( state_stack )
	outreg2 using Table6, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append

***************************************************************
************************   Table 7   **************************	
***************************************************************

********************* Panel A: SBA Loans **********************

* open file

	use "$code/total_stack_county.dta", clear
	global wcontrols wlnpop1 wlnmfi wmed_age wunempr wlfpr1 wminority_pop  hpi_growth

	reghdfe log_sba_amount ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table7a, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel replace
	reghdfe log_sba_loan_amount ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table7a, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	reghdfe log_sba_rev_amount ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table7a, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append

******************** Panel B: FinTech Loans ********************

	reghdfe log_sbo_loan_vol ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table7b, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel replace
	ppmlhdfe fintech ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table7b, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	ppmlhdfe total_nonbank ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table7b, label addtext(Tract * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append

***************************************************************
************************   Table 8   **************************	
***************************************************************

* open file

	use "$code/sipp_exemptions.dta", clear

* degree of coverage

	reghdfe log_bus adj_exempt inctot networth age, absorb(college-married id state_yr) cluster(fips_st)
	outreg2 using Table8, label addtext(Individual FE , Yes, College FE , Yes, Married FE , Yes, State * Year , Yes) tstat bdec(3) excel replace
	reghdfe log_cc adj_exempt inctot networth age, absorb(college-married id state_yr) cluster(fips_st)
	outreg2 using Table8, label addtext(Individual FE , Yes, College FE , Yes, Married FE , Yes, State * Year , Yes) tstat bdec(3) excel append
	reghdfe log_home adj_exempt inctot networth age, absorb(college-married id state_yr) cluster(fips_st)
	outreg2 using Table8, label addtext(Individual FE , Yes, College FE , Yes, Married FE , Yes, State * Year , Yes) tstat bdec(3) excel append
	reghdfe log_ast adj_exempt inctot networth age, absorb(college-married id state_yr) cluster(fips_st)
	outreg2 using Table8, label addtext(Individual FE , Yes, College FE , Yes, Married FE , Yes, State * Year , Yes) tstat bdec(3) excel append

* covered dummy

	reghdfe log_bus covered inctot networth age, absorb(college-married id state_yr) cluster(fips_st)
	outreg2 using Table8, label addtext(Individual FE , Yes, College FE , Yes, Married FE , Yes, State * Year , Yes) tstat bdec(3) excel append
	reghdfe log_cc covered inctot networth age, absorb(college-married id state_yr) cluster(fips_st)
	outreg2 using Table8, label addtext(Individual FE , Yes, College FE , Yes, Married FE , Yes, State * Year , Yes) tstat bdec(3) excel append
	reghdfe log_home covered inctot networth age, absorb(college-married id state_yr) cluster(fips_st)
	outreg2 using Table8, label addtext(Individual FE , Yes, College FE , Yes, Married FE , Yes, State * Year , Yes) tstat bdec(3) excel append
	reghdfe log_ast covered inctot networth age, absorb(college-married id state_yr) cluster(fips_st)
	outreg2 using Table8, label addtext(Individual FE , Yes, College FE , Yes, Married FE , Yes, State * Year , Yes) tstat bdec(3) excel append

***************************************************************
************************   Table 9   **************************	
***************************************************************

* open file

	use "$code/total_stack_county.dta", clear
	global wcontrols wlnpop1 wlnmfi wmed_age wunempr wlfpr1 wminority_pop  hpi_growth

* establishments 

	ppmlhdfe n1_19 ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table9, label addtext(County * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel replace
	ppmlhdfe n20_49 ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table9, label addtext(County * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	ppmlhdfe n50_249 ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table9, label addtext(County * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	ppmlhdfe n250_499 ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table9, label addtext(County * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	
* employment

	reghdfe log_empendq4_total0_19_ ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table9, label addtext(County * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	reghdfe log_empendq4_total20_49_ ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table9, label addtext(County * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	reghdfe log_empendq4_total50_249_ ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table9, label addtext(County * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
	reghdfe log_empendq4_total250_499_ ln_homestead_dec31 $wcontrols if year>2005, absorb(c_county c_msa_year2) cluster(state_stack)
	outreg2 using Table9, label addtext(County * Cohort FE , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append

***************************************************************
************************   Table 10   *************************	
***************************************************************

* open file
	use "$code/total_stack_county_ind.dta", clear
	global wcontrols wlnpop1 wlnmfi wmed_age wunempr wlfpr1 wminority_pop  hpi_growth2

* establishments by external dependence on finance
	
	ppmlhdfe n1_19 c.ln_homestead_dec31##i.high_exdep2 $wcontrols if year>2005 & (high_exdep2==1 | low_exdep2==1), absorb(c_county c_naics2 c_msa_year2) cluster(state_stack)
	outreg2 using Table10, label addtext(County * Cohort FE , Yes, NAICS2 * Cohort , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel replace

* employment by external dependence on finance
	
	reghdfe log_empendq4_total0_19_ c.ln_homestead_dec31##i.high_exdep2 $wcontrols if year>2005 & (high_exdep2==1 | low_exdep2==1), absorb(c_county c_naics2 c_msa_year2) cluster(state_stack)			
	outreg2 using Table10, label addtext(County * Cohort FE , Yes, NAICS2 * Cohort , Yes, MSA * Year * Cohort , Yes) tstat bdec(3) excel append
